package pub.strawhat.album.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;

import pub.strawhat.album.model.FileVO;
import pub.strawhat.album.model.TopicsVO;
import pub.strawhat.album.service.FileService;
import pub.strawhat.album.service.TopicsService;

@Controller
public class TopicsController {
	@Autowired
	private TopicsService topicsService;
	@Autowired
	private FileService fileService;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	@GetMapping(value = "admin/topics/list")
	public String list(Model model) {
		model.addAttribute("active", "topics");
		return "admin/topics/list";
	}

	@ResponseBody
	@GetMapping(value = "admin/topics/page")
	public PageInfo<TopicsVO> page(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum,
			TopicsVO topics) {
		PageInfo<TopicsVO> page = topicsService.findTopicsList(topics, pageNum, pageSize);
		return page;
	}

	@GetMapping(value = "admin/topics/add")
	public String add(Model model) {
		model.addAttribute("topicsId", topicsService.getTopicsId());
		return "admin/topics/add";
	}

	@GetMapping(value = "admin/topics/edit/{id}")
	public String update(@PathVariable("id") Integer id, Model model) {
		TopicsVO topics = topicsService.findTopicsById(id);
		List<FileVO> files = fileService.findFileListByBatchId(id);
		topics.setFiles(files);
		model.addAttribute("topics", topics);
		return "admin/topics/edit";
	}

	@PostMapping(value = "admin/topics/update")
	public String doUpdate(TopicsVO topics, RedirectAttributes redirectAttributes) {
		topicsService.updateTopics(topics);
		redirectAttributes.addFlashAttribute("message", "更新成功！");
		return "redirect:/admin/topics/list";
	}

	@PostMapping(value = "admin/topics/add")
	public String doAdd(TopicsVO topics, RedirectAttributes redirectAttributes) {
		topicsService.saveTopics(topics);
		redirectAttributes.addFlashAttribute("message", "创建成功！");
		return "redirect:/admin/topics/list";
	}

	@ResponseBody
	@GetMapping(value = "admin/topics/delete/{id}")
	public Boolean del(@PathVariable("id") Integer id) {
		return topicsService.delTopics(id);
	}

	@ResponseBody
	@PostMapping(value = "admin/topics/upload")
	public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request)
			throws IllegalStateException, IOException {
		String fileName = file.getOriginalFilename();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		String projectPath = request.getSession().getServletContext().getRealPath("/");
		String filePath = "/upload/" + sdf.format(new Date()) + "/";
		String newFileName = uuid + fileName.substring(fileName.lastIndexOf("."));
		File fileDir = new File(projectPath + filePath);
		if (!fileDir.exists())
			fileDir.mkdirs();
		file.transferTo(new File(projectPath + filePath, newFileName));
		FileVO fileVO = new FileVO();
		fileVO.setFileName(fileName);
		fileVO.setFileType(file.getContentType());
		fileVO.setFileSize(file.getSize());
		fileVO.setFilePath(filePath + newFileName);
		Integer batchId = Integer.valueOf(request.getParameter("batchId"));
		fileVO.setBatchId(batchId);
		fileService.saveFile(fileVO);
		return "sys/upload";
	}

	@ResponseBody
	@GetMapping(value = "test")
	public String test(HttpServletRequest request) {
		// String fileName = "示例图片_01.jpg";
		// String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		// String path =
		// request.getSession().getServletContext().getRealPath("\\");
		// String filePath = "\\upload\\" + sdf.format(new Date()) + "\\" +
		// "6dbfe70166934e018050794b444dfef5"
		// + fileName.substring(fileName.lastIndexOf("."));
		// System.out.println(path + filePath);
		// File f = new File(path + filePath);
		// if (!f.exists())
		// f.mkdirs();
		// File f2 = new File(path + filePath);
		return "sys/upload";
	}
}